﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Xml.Linq;

namespace POS
{
    public partial class TableChart : DevExpress.XtraEditors.XtraUserControl
    {
        Dictionary<int, SimpleButton> TableButtonList = new Dictionary<int, SimpleButton>();
        
        public TableChart()
        {
            StaticProperties.GetDateCode();
            InitializeComponent();
            int x = 1;
            // read in the entire document
            var document = XDocument.Parse(StaticProperties.TableChartXML);//.Load(filePath);

            var champs = document.Descendants("SimpleButton")
                .Select(e => new { text = e.Element("Text").Value, X = e.Element("X").Value, Y = e.Element("Y").Value, Width = e.Element("Width").Value, Height = e.Element("Height").Value });

            foreach (var champ in champs)
            {
                SimpleButton test = new SimpleButton();
                
                //int locationXextra = (340 / 2) + 50;
                test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                test.Name = "btn" + x.ToString();
                test.Text = champ.text;
                test.Width = Convert.ToInt32(champ.Width);
                test.Height = Convert.ToInt32(champ.Height);
                test.Location = new Point(Convert.ToInt32(champ.X), Convert.ToInt32(champ.Y));
                test.MouseClick += new MouseEventHandler(btn_MouseClick);
                test.Appearance.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
                test.Appearance.FontStyleDelta = FontStyle.Bold;
                //MessageBox.Show(x.ToString());
                TableButtonList.Add(x, test);
                this.Controls.Add(test);

                x++;
                
            }
        }

        void btn_MouseClick(object sender, MouseEventArgs e)
        {
            var button = (SimpleButton)sender;
            StaticProperties.TableName = button.Text;

            if (button.Text == "OPEN TICKETS")
            {
                MainProgram._Main.panelControl1.Controls.Clear();
                MainProgram._Main.panelControl1.Controls.Add(new OpenTickets());
            }
            else
            {
                if (button.ForeColor == Color.Blue)
                {
                    int ModReceipt = 0; ;
                    foreach (DataRow Receipt in DataTableClass.Receipt.Rows)
                    {
                        if(!Convert.ToBoolean(Receipt["ReceiptStatus"].ToString()))
                        {
                            if(Receipt["ReceiptTableName"].ToString().Trim().ToUpper() == button.Text.ToUpper())
                            {
                                ModReceipt = Convert.ToInt32(Receipt["ReceiptNo"].ToString());
                            }
                        }
                    }

                    MainProgram._Main.panelControl1.Controls.Clear();
                    MainProgram._Main.panelControl1.Controls.Add(new OrderEntryScreen(ModReceipt));

                }
                else
                {
                    MainProgram._Main.panelControl1.Controls.Clear();
                    MainProgram._Main.panelControl1.Controls.Add(new OrderEntryScreen());
                }
            }
        }

        private void TableChart_Load(object sender, EventArgs e)
        {
            foreach (DataRow item in DataTableClass.Receipt.Rows)
            {
                //if (item["ReceiptNo"].ToString().StartsWith(StaticProperties.DateCode.ToString()))
                //{
                    if (item["ReceiptServerID"].ToString().Trim() == StaticProperties.ServerID.ToString())
                    {
                        if (Convert.ToBoolean(item["ReceiptStatus"].ToString()) == false)
                        {
                            foreach (KeyValuePair<int, SimpleButton> buttonList in TableButtonList)
                            {
                                if (buttonList.Value.Text == item["ReceiptTableName"].ToString().Trim())
                                {
                                    buttonList.Value.ForeColor = Color.Blue;
                                }
                            }
                        }
                    }
                //}
            }
        }
    }
}
